package org.gnomus.server.controllers;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import org.gnomus.data.store;
import org.gnomus.server.Context;
import org.gnomus.server.Controller;

import java.util.*;

public class Teach implements Controller {
    public String id;
    public String user_id;
    public String lesson_id;
    public String class_title;

    public boolean index() {
/*
        Context ctx = Context.get();
        if (ctx.account == null) {
            ctx.redirect(Context.us.createLoginURL(ctx.uri));
        }
        Entity e = store.get("account", (String) ctx.account.get("id"));
        if (e == null) {
            return true;
        }
        List<Map> klasses = new ArrayList<Map>();
        Iterable<Entity> class_links = store.link.get("class", e.getKey(), null);
        for (Entity link : class_links) {
            Entity klass = store.get((Key) link.getProperty("to"));
            klasses.add(klass.getProperties());
        }
        ctx.data.put("class_list", klasses);
*/
        return true;
    }

    public boolean klass() {
/*
        Context ctx = Context.get();
        if (id == null) {
            ctx.redirect("/teach");
        }
        Entity klass = store.get("class", id);
        if (klass == null) {
            ctx.errors.add("Class not found.");
            return true;
        }
        Map<String, Object> class_data = new HashMap<String, Object>(klass.getProperties());
        List<Map> students = new ArrayList<Map>();
        List<Map> assignments = new ArrayList<Map>();
        Iterable<Entity> student_links = store.link.get("student", klass.getKey(), null);
        for (Entity link : student_links) {
            Entity student = store.get((Key) link.getProperty("to"));
            students.add(student.getProperties());
        }
        Iterable<Entity> assignment_links = store.link.get("assignment", klass.getKey(), null);
        for (Entity link : assignment_links) {
            Entity assignment = store.get((Key) link.getProperty("to"));
            assignments.add(assignment.getProperties());
        }
        class_data.put("students", students);
        class_data.put("assignments", assignments);
        ctx.data.put("class", class_data);


        Iterable<Entity> users = store.get("account");
        List<Map> user_list = new ArrayList<Map>();
        for (Entity user : users) {
            user_list.add(user.getProperties());
        }
        ctx.data.put("user_list", user_list);

        Iterable<Entity> lessons = store.get("lesson");
        List<Map> lesson_list = new ArrayList<Map>();
        for (Entity lesson : lessons) {
            lesson_list.add(lesson.getProperties());
        }
        ctx.data.put("lesson_list", lesson_list);
*/
        return true;
    }

    public boolean createClass() {
/*
        Context ctx = Context.get();
        ctx.ajax = true;
        if (!"POST".equals(ctx.method) || ctx.account == null || class_title == null) {
            return false;
        }
        Entity user = store.get(store.s2k((String) ctx.account.get("k")));
        if (user == null) {
            return false;
        }
        Map klass = new HashMap();
        klass.put("title", class_title);
        Key ck = store.put("class", klass);
        store.link.put("class", user.getKey(), ck);
        ctx.redirect("/teach");
*/
        return true;
    }

    public boolean addStudent() {
/*
        Context ctx = Context.get();
        ctx.ajax = true;
        if (!"POST".equals(ctx.method) || id == null || user_id == null) {
            return true;
        }
        Entity klass = store.get("class", id);
        Entity user = store.get("account", user_id);
        if (klass != null && user != null) {
            Boolean link_exists = store.link.get("student", klass.getKey(), user.getKey()).iterator().hasNext();
            if (!link_exists) {
                store.link.put("student", klass.getKey(), user.getKey());
            }
        }
        ctx.redirect("/teach/class?id=" + id);
*/
        return true;
    }

    public boolean createAssignment() {
/*
        Context ctx = Context.get();
        ctx.ajax = true;
        if (!"POST".equals(ctx.method) || id == null || lesson_id == null) {
            return true;
        }
        Entity klass = store.get("class", id);
        Entity lesson = store.get("lesson", lesson_id);
        if (klass != null && lesson != null) {
            boolean link_exists = store.link.get("assignment", klass.getKey(), lesson.getKey()).iterator().hasNext();
            if (!link_exists) {
                store.link.put("assignment", klass.getKey(), lesson.getKey());
            }
        }
        ctx.redirect("/teach/class?id=" + id);
*/
        return true;
    }
}
